package com.fsj.miniapp.miniapp.api.impl;

import com.fsj.miniapp.miniapp.api.WxMaQrcodeService;
import com.fsj.miniapp.miniapp.api.WxMaService;
import com.fsj.miniapp.miniapp.bean.WxMaQrcode;
import com.fsj.miniapp.miniapp.bean.WxMaWxcode;
import com.fsj.miniapp.miniapp.bean.WxMaWxcodeLimit;
import com.fsj.miniapp.miniapp.util.http.QrCodeRequestExecutor;
import me.chanjar.weixin.common.exception.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.net.URL;

/**
 * @author cja
 */
@Service("qrCodeService")
public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
    @Autowired
    private WxMaService wxMaService;

    public WxMaQrcodeServiceImpl(WxMaService wxMaService) {
        this.wxMaService = wxMaService;
    }

    @Override
    public File createQrcode(String path, int width) throws WxErrorException {
        String url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode";
        return this.wxMaService.execute(new QrCodeRequestExecutor(this.wxMaService.getRequestHttp()),
                url, new WxMaQrcode(path, width));
    }

    @Override
    public File createQrcode(String path) throws WxErrorException {
        return this.createQrcode(path, 430);
    }

    @Override
    public File createWxCode(String path, int width, boolean autoColor, LineColor lineColor) throws WxErrorException {
        String url = "https://api.weixin.qq.com/wxa/getwxacode";
        WxMaWxcode wxMaWxcode = new WxMaWxcode();
        wxMaWxcode.setPath(path);
        wxMaWxcode.setWidth(width);
        wxMaWxcode.setAutoColor(autoColor);
        wxMaWxcode.setLineColor(lineColor);
        return this.wxMaService.execute(new QrCodeRequestExecutor(this.wxMaService.getRequestHttp()),
                url, wxMaWxcode);
    }

    @Override
    public File createWxCode(String path, int width) throws WxErrorException {
        return this.createWxCode(path, width, true, null);
    }

    @Override
    public File createWxCode(String path) throws WxErrorException {
        return this.createWxCode(path, 430, true, null);
    }

    @Override
    public File createWxCodeLimit(String scene, String page, int width, boolean autoColor, LineColor lineColor) throws WxErrorException {
        String url = "http://api.weixin.qq.com/wxa/getwxacodeunlimit";
        WxMaWxcodeLimit wxMaWxcodeLimit = new WxMaWxcodeLimit();
        wxMaWxcodeLimit.setScene(scene);
        wxMaWxcodeLimit.setPage(page);
        wxMaWxcodeLimit.setWidth(width);
        wxMaWxcodeLimit.setAutoColor(autoColor);
        wxMaWxcodeLimit.setLineColor(lineColor);
        return this.wxMaService.execute(new QrCodeRequestExecutor(this.wxMaService.getRequestHttp()),
                url, wxMaWxcodeLimit);
    }

    @Override
    public File createWxCodeLimit(String scene, String page) throws WxErrorException {
        return this.createWxCodeLimit(scene, page, 430, true, null);
    }

    public InputStream changeLogo(String userImg, File miniImg) {

        try {
            URL userUrl = new URL(userImg);
            BufferedImage image = ImageIO.read(userUrl);
            BufferedImage output = new BufferedImage(195, 195, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = output.createGraphics();
            output = g2.getDeviceConfiguration().createCompatibleImage(195, 195, Transparency.TRANSLUCENT);
            g2.dispose();
            g2 = output.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            g2.fillRoundRect(0, 0, 195, 195, 800, 800);
            g2.setComposite(AlphaComposite.SrcIn);
            g2.drawImage(image.getScaledInstance(image.getWidth(), image.getHeight(), BufferedImage.SCALE_SMOOTH), 0, 0, 195, 195, null);
            g2.dispose();

            //此处是小程序码的路径
            BufferedImage appletImg = ImageIO.read(miniImg);
            Graphics2D g2d = appletImg.createGraphics();

            // 设置抗锯齿的属性
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            //此处是替换logo的临时图片路径
            BufferedImage centerImg = output;
            g2d.drawImage(centerImg.getScaledInstance(centerImg.getWidth(), centerImg.getHeight(), BufferedImage.SCALE_SMOOTH), (appletImg.getWidth() - centerImg.getWidth()) / 2, (appletImg.getHeight() - centerImg.getHeight()) / 2, null);

            // 关闭资源
            g2d.dispose();

            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(appletImg, "png", os);
            InputStream is = new ByteArrayInputStream(os.toByteArray());

            return is;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

}
